﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace Client
{
    public partial class FormAddDevice : BasePopForm
    {
        QunYing.BLL.VipInfo bll_VipInfo = new QunYing.BLL.VipInfo();
        QunYing.BLL.ICSA_Device bll_icsa = new QunYing.BLL.ICSA_Device();
        QunYing.BLL.DeviceInfo bll_device = new QunYing.BLL.DeviceInfo();
        QunYing.Model.VipInfo vipInfo;
        public FormAddDevice()
        {
            InitializeComponent();
            this.FormValidator.SetControlValitors(this.txt_通信号码, Spore.Validator.ValidatorDefaults.NotNull, Spore.Validator.ValidatorDefaults.Phone);
        }

        public void BindData(QunYing.Model.VipInfo vipInfo)
        {
            this.vipInfo = vipInfo;
            this.lbl_会员卡号.Text = vipInfo.会员卡号;
        }

        private void btn_提交_Click(object sender, EventArgs e)
        {
            if (!this.FormValidator.ValidAll()) return;
            try
            {
                var icsa = bll_icsa.GetModel(this.txt_通信号码.Text.Trim());
                if (icsa == null)
                {
                    GlobalInfo.MsgBoxShowError("该设备不存在！");
                    return;
                }
                if (icsa.是否已售 != null)
                {
                    if (icsa.是否已售.Value)
                    {
                        GlobalInfo.MsgBoxShowError("该设备已出售！");
                        return;
                    }
                }
                icsa.是否已售 = true;
                icsa.报警接收手机号码 = vipInfo.移动电话;
                icsa.所属经销商 = vipInfo.经销商编号;
                QunYing.Model.DeviceInfo dif = new QunYing.Model.DeviceInfo();
                dif.ICSA编号 = icsa.通信号码;
                dif.名称 = icsa.通信号码 + "鱼缸";
                dif.品牌 = "未设置";
                dif.水草品种 = "未设置";
                dif.水族箱地址 = "未设置";
                dif.箱体尺寸 = "未设置";
                dif.型号 = "未设置";
                dif.鱼类品种 = "未设置";
                icsa.水族箱ID = bll_device.Add(dif);
                bll_icsa.Update(icsa);
                vipInfo.水族箱设备ID = vipInfo.水族箱设备ID.Trim(",".ToCharArray()) + "," + icsa.通信号码;
                this.bll_VipInfo.Update(vipInfo);
                this.DialogResult = DialogResult.OK;
            }
            catch (Exception ex)
            {
                GlobalInfo.MsgBoxShowError("增加设备失败。");
                QunYing.Common.Loger.Log.Error("Vip新增设备时失败："+ex.Message);
            }
            

        }

        private void btn_关闭_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        
    }
}